package qp.src.app.client.widget;

import qp.src.app.shared.DataBeanProxy;
import qp.src.app.shared.DataBeanRequest;
import qp.src.app.shared.DataGridRequestFactory;
import qp.src.app.shared.SubDataBeanProxy;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.web.bindery.requestfactory.shared.Receiver;

public abstract class EditPopup {

  private PageMode popupMode; // จะต้องมีค่าเสมอ

  private DataGridRequestFactory factory;
  private DataBeanRequest request;
  private DataBeanProxy proxy;


  private PopupListener popupListener;

  private DialogBox dialogBox;

  private TextBox name;

  private TextBox lastName;

  private TextArea addressText;

  private TextBox subNameText;
  private TextBox subLastNameText;

  private ClickHandler EDIT_CLICK_HANDLER = new ClickHandler() {
    public void onClick(ClickEvent event) {
      request = factory.dataBeanRequest();
      DataBeanProxy editProxy = request.edit( proxy );
      editProxy.setAddress( addressText.getText() );
      editProxy.setLastName( lastName.getText() );
      editProxy.setName( name.getText() );

      SubDataBeanProxy subDataProxy = editProxy.getData();
      if( subDataProxy != null ){
        subDataProxy.setLastName( subLastNameText.getValue() );
        subDataProxy.setName( subNameText.getValue() );
      }
      else{
        SubDataBeanProxy proxy = request.create(SubDataBeanProxy.class);
        proxy.setLastName( subLastNameText.getValue() );
        proxy.setName( subNameText.getValue() );
        editProxy.setData( proxy );
      }

      request.persist( editProxy );
      request.fire(new Receiver<Void>() {

        @Override
        public void onSuccess(Void response) {
          closePopup();
        }
      });
    }
  };


  public EditPopup() {

    dialogBox = new DialogBox();
    dialogBox.ensureDebugId("edit");
    dialogBox.setGlassEnabled(true);
    dialogBox.setAnimationEnabled(true);
    dialogBox.setText("แก้ไข");

    VerticalPanel dialogContents = new VerticalPanel();
    dialogContents.setSpacing(2);
    dialogContents.setHeight("300");
    dialogContents.setWidth("300");
    dialogBox.setWidget(dialogContents);

    HTML details = new HTML("การแก้ไขข้อมูล");
    dialogContents.add(details);
    dialogContents.setCellHorizontalAlignment(details,
        HasHorizontalAlignment.ALIGN_CENTER);

    name = new TextBox();
    lastName = new TextBox();
    addressText = new TextArea();
    subNameText = new TextBox();
    subNameText.setTitle("Sub Name");
    subNameText.ensureDebugId("subNameText");
    subLastNameText = new TextBox();
    subLastNameText.setTitle("Sub Last Name");

    final DateBox dateBox = new DateBox();

    DateTimeFormat dateFormat = DateTimeFormat.getLongDateFormat();
    dateBox.setFormat(new DateBox.DefaultFormat(dateFormat));

    name.setTitle("Name");
    lastName.setTitle("LastName");
    dialogContents.add(name);
    dialogContents.add(lastName);
    dialogContents.add(addressText);
    dialogContents.add(dateBox);
    dialogContents.add(subNameText);
    dialogContents.add(subLastNameText);

    Button editButton = new Button("Edit", EDIT_CLICK_HANDLER);

    Button closeButton = new Button("Close", new ClickHandler() {
      public void onClick(ClickEvent event) {
        closePopup();
      }
    });

    dialogContents.add(editButton);
    dialogContents.add(closeButton);

    if (LocaleInfo.getCurrentLocale().isRTL()) {
      dialogContents.setCellHorizontalAlignment(closeButton,
          HasHorizontalAlignment.ALIGN_LEFT);
    }
    else {
      dialogContents.setCellHorizontalAlignment(closeButton,
          HasHorizontalAlignment.ALIGN_RIGHT);
    }
  }

  // ****************************************************************************

  public void openPopup() {
    openPopup(null, null, null);
  }

  public void openPopup(PageMode popupMode) {
    openPopup(popupMode, null, null);
  }

  public void openPopup(PageMode popupMode, Object popupParameter) {
    openPopup(popupMode, popupParameter, null);
  }

  public final void openPopup(
    PageMode popupMode,
    Object popupParameter,
    PopupListener popupListener) {
    this.popupMode = popupMode;
    this.popupListener = popupListener;

    Object [] object = (Object[])popupParameter;
    this.factory = (DataGridRequestFactory) object[0];
    this.proxy = (DataBeanProxy) object[1];

    name.setValue("");
    lastName.setValue("");
    addressText.setValue("");
    subNameText.setValue("");
    subLastNameText.setValue("");

    name.setValue(proxy.getName());
    lastName.setValue(proxy.getLastName());
    addressText.setValue(proxy.getAddress());
    SubDataBeanProxy subDataProxy = proxy.getData();
    if( subDataProxy != null  ){
      System.out.println("subDataProxy.getName()="+subDataProxy.getName());
      subNameText.setValue(subDataProxy.getName());
      subLastNameText.setValue(subDataProxy.getLastName());
    }

    dialogBox.setTitle("AAAAAA");
    dialogBox.center();
    dialogBox.show();
  }

  // ****************************************************************************

  protected final void closePopup() {
    dialogBox.hide();
  }

  protected void closePopup(Object result) {
    closePopup();

    if (popupListener != null) {
      popupListener.onReturn(result);
    }
  }
}
